package com.ct.application.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.goods.convert.GoodsConvert;
import com.ct.application.goods.model.po.GoodsAddPO;
import com.ct.application.goods.model.po.GoodsQueryPO;
import com.ct.application.goods.model.po.GoodsUpdatePO;
import com.ct.application.goods.model.vo.GoodsVO;
import com.ct.goods.orm.Goods;
import com.ct.goods.responsity.admin.IAdminGoodsRepository;
import com.ct.repository.commons.PageUtils;
import com.ct.shops.orm.Shops;
import com.ct.shops.repository.admin.IAdminShopsRepository;
import com.ct.utils.valid.ValidatorUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 商品表 服务实现类
 *
 * @author system
 */
@Service
public class GoodsService {


    @Resource
    private IAdminGoodsRepository goodsDao;
    @Resource
    private IAdminShopsRepository shopsDao;
    @Resource
    private GoodsConvert goodsConvert;


    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return goodsDao.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return goodsDao.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }

    /**
     * 获取商品列表数据
     */
    public PageUtils<GoodsVO> getGoodsList(GoodsQueryPO queryPO) {
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(queryPO.getKeyword()), Goods::getName, queryPO.getKeyword());
        lambdaQueryWrapper.eq(!Objects.isNull(queryPO.getStatus()), Goods::getStatus, queryPO.getStatus());
        lambdaQueryWrapper.orderByDesc(Goods::getCreateTime);
        Page<Goods> page = new Page<>(queryPO.getPageNo(), queryPO.getPageSize());
        IPage<Goods> goodsIPage = goodsDao.page(page, lambdaQueryWrapper);

        List<Goods> goodsList = goodsIPage.getRecords();
        if (goodsList.isEmpty()) {
            return PageUtils.getEmptyInstance();
        }
        List<GoodsVO> goodsVOList = goodsConvert.toGoodsVOList(goodsList);
        return new PageUtils<>(goodsIPage, goodsVOList);
    }


    /**
     * 根据id查询商品
     */
    public GoodsVO getGoodsById(Long id) {
        Goods goods = goodsDao.getById(id);
        if (goods == null) {
            return null;
        }
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods,goodsVO);
        return goodsVO;
    }


    /**
     * 新增商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(GoodsAddPO addPO) {
        Long shopsId = addPO.getShopsId();
        Shops shops;
        if (shopsId == null || (shops = shopsDao.getById(shopsId))== null) {
            throw new RuntimeException("门店不能为空");
        }

        Goods goods = new Goods();
        BeanUtils.copyProperties(addPO, goods);
        goods.setShopsId(shopsId);
        goods.setShopsName(shops.getName());

        return goods.insert();
    }


    /**
     * 修改商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(GoodsUpdatePO updatePO) {
        Long shopsId = updatePO.getShopsId();
        Shops shops;
        if (shopsId == null || (shops = shopsDao.getById(shopsId))== null) {
            throw new RuntimeException("门店不能为空");
        }

        Goods goods = new Goods();
        BeanUtils.copyProperties(updatePO, goods);
        goods.setShopsId(shopsId);
        goods.setShopsName(shops.getName());

        return goods.updateById();
    }


    /**
     * 删除商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return goodsDao.removeById(id);
    }

}
